package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.domain.SysDictType;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysDictTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 广电用户标签数据字典初始化器
 * 系统启动时自动初始化广电用户分析所需的标签数据字典
 */
@Component
@Order(100)
public class GuangDianDictDataInitializer implements ApplicationRunner {

    @Autowired
    private SysDictTypeMapper dictTypeMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    private static final String CREATE_BY = "system";
    private static final String STATUS_NORMAL = "0";

    @Override
    @Transactional
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("开始初始化广电用户标签数据字典...");
        
        // 初始化字典类型
        initDictTypes();
        
        // 初始化字典数据
        initDictData();
        
        System.out.println("广电用户标签数据字典初始化完成！");
    }

    /**
     * 初始化字典类型
     */
    private void initDictTypes() {
        // 字典类型数据
        SysDictType[] dictTypes = {
            createDictType(10001L, "是否为重度用户", "guangdian_heavy_user", "广电用户标签-是否为重度用户"),
            createDictType(10002L, "电视消费水平", "guangdian_tv_consumption", "广电用户标签-电视消费水平"),
            createDictType(10003L, "宽带消费水平", "guangdian_broadband_consumption", "广电用户标签-宽带消费水平"),
            createDictType(10004L, "用户是否忠诚", "guangdian_loyal_user", "广电用户标签-用户是否忠诚"),
            createDictType(10005L, "业务品牌", "guangdian_service_brand", "广电用户标签-业务品牌"),
            createDictType(10006L, "电视入网程度", "guangdian_tv_penetration", "广电用户标签-电视入网程度"),
            createDictType(10007L, "宽带入网程度", "guangdian_broadband_penetration", "广电用户标签-宽带入网程度"),
            createDictType(10008L, "用户类型", "guangdian_user_type", "广电用户标签-用户类型")
        };

        // 插入或更新字典类型
        for (SysDictType dictType : dictTypes) {
            SysDictType existing = dictTypeMapper.selectDictTypeByType(dictType.getDictType());
            if (existing == null) {
                dictTypeMapper.insertDictType(dictType);
                System.out.println("插入字典类型: " + dictType.getDictName());
            } else {
                // 更新现有记录
                dictType.setDictId(existing.getDictId());
                dictTypeMapper.updateDictType(dictType);
                System.out.println("更新字典类型: " + dictType.getDictName());
            }
        }
    }

    /**
     * 初始化字典数据
     */
    private void initDictData() {
        // 1. 重度用户标签数据
        createDictData("guangdian_heavy_user", 1L, "是", "是", "", "", "N", "重度用户");
        createDictData("guangdian_heavy_user", 2L, "否", "否", "", "", "Y", "非重度用户");

        // 2. 电视消费水平标签数据
        createDictData("guangdian_tv_consumption", 1L, "高", "高", "primary", "", "N", "高消费水平");
        createDictData("guangdian_tv_consumption", 2L, "中", "中", "success", "", "Y", "中等消费水平");
        createDictData("guangdian_tv_consumption", 3L, "低", "低", "info", "", "N", "低消费水平");

        // 3. 宽带消费水平标签数据
        createDictData("guangdian_broadband_consumption", 1L, "高", "高", "primary", "", "N", "高消费水平");
        createDictData("guangdian_broadband_consumption", 2L, "中", "中", "success", "", "Y", "中等消费水平");
        createDictData("guangdian_broadband_consumption", 3L, "低", "低", "info", "", "N", "低消费水平");

        // 4. 用户忠诚度标签数据
        createDictData("guangdian_loyal_user", 1L, "是", "是", "success", "", "N", "忠诚用户");
        createDictData("guangdian_loyal_user", 2L, "否", "否", "info", "", "Y", "非忠诚用户");

        // 5. 业务品牌标签数据
        createDictData("guangdian_service_brand", 1L, "单业务", "单业务", "info", "", "Y", "仅使用一种业务");
        createDictData("guangdian_service_brand", 2L, "双业务", "双业务", "success", "", "N", "使用两种业务");
        createDictData("guangdian_service_brand", 3L, "多业务", "多业务", "primary", "", "N", "使用三种及以上业务");

        // 6. 电视入网程度标签数据
        createDictData("guangdian_tv_penetration", 1L, "高", "高", "primary", "", "N", "高度入网");
        createDictData("guangdian_tv_penetration", 2L, "中", "中", "success", "", "Y", "中度入网");
        createDictData("guangdian_tv_penetration", 3L, "低", "低", "info", "", "N", "低度入网");

        // 7. 宽带入网程度标签数据
        createDictData("guangdian_broadband_penetration", 1L, "高", "高", "primary", "", "N", "高度入网");
        createDictData("guangdian_broadband_penetration", 2L, "中", "中", "success", "", "Y", "中度入网");
        createDictData("guangdian_broadband_penetration", 3L, "低", "低", "info", "", "N", "低度入网");

        // 8. 用户类型标签数据
        createDictData("guangdian_user_type", 1L, "老用户", "老用户", "primary", "", "N", "在网6年以上用户");
        createDictData("guangdian_user_type", 2L, "中端用户", "中端用户", "success", "", "N", "在网2-6年用户");
        createDictData("guangdian_user_type", 3L, "新用户", "新用户", "info", "", "Y", "在网2年以下用户");
    }

    /**
     * 创建字典类型对象
     */
    private SysDictType createDictType(Long dictId, String dictName, String dictType, String remark) {
        SysDictType dictTypeObj = new SysDictType();
        dictTypeObj.setDictId(dictId);
        dictTypeObj.setDictName(dictName);
        dictTypeObj.setDictType(dictType);
        dictTypeObj.setStatus(STATUS_NORMAL);
        dictTypeObj.setCreateBy(CREATE_BY);
        dictTypeObj.setCreateTime(DateUtils.getNowDate());
        dictTypeObj.setRemark(remark);
        return dictTypeObj;
    }

    /**
     * 创建并插入字典数据
     */
    private void createDictData(String dictType, Long dictSort, String dictLabel, String dictValue, 
                               String cssClass, String listClass, String isDefault, String remark) {
        // 检查是否已存在
        SysDictData condition = new SysDictData();
        condition.setDictType(dictType);
        condition.setDictValue(dictValue);
        List<SysDictData> existingData = dictDataMapper.selectDictDataList(condition);

        SysDictData dictData = new SysDictData();
        dictData.setDictSort(dictSort);
        dictData.setDictLabel(dictLabel);
        dictData.setDictValue(dictValue);
        dictData.setDictType(dictType);
        dictData.setCssClass(cssClass);
        dictData.setListClass(listClass);
        dictData.setIsDefault(isDefault);
        dictData.setStatus(STATUS_NORMAL);
        dictData.setCreateBy(CREATE_BY);
        dictData.setCreateTime(DateUtils.getNowDate());
        dictData.setRemark(remark);

        if (existingData.isEmpty()) {
            dictDataMapper.insertDictData(dictData);
            System.out.println("插入字典数据: " + dictType + " - " + dictLabel);
        } else {
            // 更新现有记录
            dictData.setDictCode(existingData.get(0).getDictCode());
            dictDataMapper.updateDictData(dictData);
            System.out.println("更新字典数据: " + dictType + " - " + dictLabel);
        }
    }
}